const { Controller } = require('egg');
module.exports = class extends Controller {
    async success(message, data) {
        this.ctx.body = {
            success: true,
            message,
            data
        };
    }
    async index() {
        const { ctx, service } = this;
        const result = await service[this.entity].index(ctx.query);
        this.success('查询成功', result);
    }

    async show() {
        const { ctx, service } = this;
        const id = ctx.params.id;
        const entity = await service[this.entity].show(id);
        this.success('查询成功', entity);
    }

    async create() {
        const { ctx, service } = this;
        const entity = ctx.request.body;
        await service[this.entity].create(entity);
        this.success('创建成功');
    }

    async update() {
        const { ctx, service } = this;
        const entity = ctx.request.body;
        entity.id = ctx.params.id;
        await service[this.entity].update(entity);
        this.success('更新成功');
    }
    async destroy() {
        const { ctx, service } = this;
        const id = ctx.params.id;
        let ids = ctx.request.body;
        if (!ids) { ids = [id] }
        await service[this.entity].delete(ids);
        this.success('删除成功');
    }
}
